package com.virtium.vpm.controller;

import java.util.ArrayList;
import java.util.List;

import com.virtium.vpm.dao.DAOFactory;
import com.virtium.vpm.dao.MajorTaskDAO;
import com.virtium.vpm.dao.ProjectDAO;
import com.virtium.vpm.form.ProjectRowForm;
import com.virtium.vpm.hibernate.model.MajorTask;
import com.virtium.vpm.hibernate.model.Project;
import com.virtium.vpm.util.DateUtil;


public class ProjectManager {
	private int rowCount = 5;	
	public ArrayList<ArrayList> getAllListProject(int page){
		ArrayList<ArrayList> arrTables = new ArrayList<ArrayList>();
		ArrayList<ArrayList> arrProjectALl = new ArrayList<ArrayList>();
		ArrayList<String[]> arrProjectInfo = new ArrayList<String[]>();
		ArrayList<ProjectRowForm> arrProject;
		
		MajorTaskDAO majorTaskDAO;
		
		try{
			//Get All record in database
			List<Project> lProject = this.all();
			
			majorTaskDAO = DAOFactory.getInstance().getMajorTaskDAO();
			List<MajorTask> lMajorTask = majorTaskDAO.findAll();
			String content[] = new String[4];
			for(int i =0; i < lProject.size(); i++){
				Project p = (Project)lProject.get(i);
				ArrayList<MajorTask> arMajorTask = MajorTaskManager.getListMajorTaskByProjectID(lMajorTask, p.getId());
				if(arMajorTask != null && arMajorTask.size() > 0){
					arrProject = new ArrayList<ProjectRowForm>();
					for(int h=0;h < arMajorTask.size();h++){						
						MajorTask majorTask = arMajorTask.get(h);
						arrProject.add(new ProjectRowForm(
								p.getId().toString(),
								majorTask.getId().toString(),
								p.getTypeId().toString(),								
								majorTask.getUserId().toString(),								
								TypeManager.getTypeNameByID(p.getTypeId()).toString(),
								majorTask.getPriority().toString(),
								majorTask.getMajorTask().toString(),								
								UserManager.getUserByID(majorTask.getUserId()).getFullName().toString(),
								DateUtil.dateTimeToStr(majorTask.getStartDate(), "MM/dd/yyyy").toString(),
								DateUtil.dateTimeToStr(majorTask.getFinishDate(), "MM/dd/yyyy").toString(),
								majorTask.getCost().toString(),
								majorTask.getAchieved().toString(),
								majorTask.getKeyIssue().toString(),
								majorTask.getDecision().toString()));						
					}
					arrProjectALl.add(arrProject);
					content[0] = p.getPurpose().toString();
					content[1] = p.getIdeaOutcome().toString();
					content[2] = p.getEmail().toString();
					content[3] = p.getArchive().toString();
					arrProjectInfo.add(content);
				}
			}
			arrTables.add(arrProjectALl);
			arrTables.add(arrProjectInfo);
		}catch(Exception ex){
			System.out.println("Error: " + ex.getMessage());
		}
		return arrTables;
	}
	
	public int addNew(Project project){
		try{
			ProjectDAO projectDAO = DAOFactory.getInstance().getProjectDAO();
			projectDAO.save(project);
			return project.getId();
		}catch(Exception ex){}
		return 0;
	}
	public int countProjectByType(List<Project> lsProject, int typeID){
		int count = 0;
		try{
			for(int i = 0; i < lsProject.size();i++){
				if(lsProject.get(i).getTypeId() == typeID)count++;
			}
		}
		catch(Exception ex){}
		return count;
	}
	public void delete(Project project){
		try{
			MajorTaskManager mtm = new MajorTaskManager();
			//Delete All major task in this project
			MajorTaskDAO majorTaskDAO = DAOFactory.getInstance().getMajorTaskDAO();
			ArrayList<MajorTask> arrMajorTasks= mtm.getListMajorTaskByProjectID(project.getId());
			for(int i = 0; i < arrMajorTasks.size();i++){
				try{
				majorTaskDAO.delete(arrMajorTasks.get(i));
				}catch(Exception ex){continue;}
			}
			//dele project
			ProjectDAO projectDAO = DAOFactory.getInstance().getProjectDAO();
			projectDAO.delete(project);		
		}catch(Exception ex){}		
	}
	
	public List<Project> all(){
		ProjectDAO projectDAO = DAOFactory.getInstance().getProjectDAO();
		return projectDAO.findAll();	
	}
	
}
